<?php namespace WoXuanWang\Advertisement\Controllers;

use App\Http\Requests\IndexRequest;
use App\Http\Requests\ShowRequest;
use Illuminate\Http\JsonResponse;
use WoXuanWang\Advertisement\Repositories\AdSlotRepository;
use WoXuanWang\Advertisement\Requests\CreateAdSlotRequest;
use WoXuanWang\Advertisement\Requests\UpdateAdSlotRequest;
use zgldh\Scaffold\AppBaseController;

class AdSlotController extends AppBaseController
{

    public function __construct(AdSlotRepository $adslotRepository)
    {
        $this->repository = $adslotRepository;
        $this->middleware("auth");
    }

    /**
     * Display a listing of the AdSlot.
     *
     * @param Request $request
     * @return Response
     */
    public function index(IndexRequest $request)
    {
        $with = $request->getWith();
        $data = $this->repository->datatables(null, $with);
        return $data;
    }

    /**
     * Store a newly created AdSlot in storage.
     *
     * @param CreateAdSlotRequest $request
     * @return JsonResponse
     */
    public function store(CreateAdSlotRequest $request)
    {
        $input = $request->all();

        $adslot = $this->repository->create($input);

        return $this->sendResponse($adslot, 'AdSlot saved successfully.');
    }

    /**
     * Display the specified AdSlot.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function show($id, ShowRequest $request)
    {
        $this->repository->with($request->getWith());
        $adslot = $this->repository->findWithoutFail($id);

        if (empty($adslot)) {
            return $this->sendError('AdSlot not found');
        }

        return $this->sendResponse($adslot, '');
    }

    /**
     * Update the specified AdSlot in storage.
     *
     * @param  int $id
     * @param UpdateAdSlotRequest $request
     *
     * @return JsonResponse
     */
    public function update($id, UpdateAdSlotRequest $request)
    {
        $adslot = $this->repository->findWithoutFail($id);

        if (empty($adslot)) {
            return $this->sendError('AdSlot not found');
        }

        $adslot = $this->repository->update($request->all(), $id);

        return $this->sendResponse($adslot, 'AdSlot updated successfully.');
    }

    /**
     * Remove the specified AdSlot from storage.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function destroy($id)
    {
        $adslot = $this->repository->findWithoutFail($id);

        if (empty($adslot)) {
            return $this->sendError('AdSlot not found');
        }

        $this->repository->delete($id);
        return $this->sendResponse($adslot, 'AdSlot deleted successfully.');
    }
}
